//@ts-ignore
import { loadEnv } from 'vite'
const envScript = process.env.npm_lifecycle_script?.split(' ') || []
const envName = envScript[envScript.length - 1] // 通过启动命令区分环境
const envData = loadEnv(envName, process.cwd())

import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'url'
import VueI18nVitePlugin from '@intlify/unplugin-vue-i18n/vite'

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  ssr: true,
  srcDir: 'src',
  compatibilityDate: '2024-04-03',
  app: {
    head: {
      charset: 'utf-8',
      viewport: 'width=device-width, initial-scale=1',
    },
    pageTransition: { name: 'page', mode: 'out-in' },
    layoutTransition: { name: 'layout', mode: 'out-in' }
  },
  devtools: { enabled: envName === 'development' },
  modules: ['@nuxt/ui',
            '@nuxt/image',
            'nuxt-icons',
            '@nuxtjs/color-mode',
            '@element-plus/nuxt',
            '@pinia/nuxt',
            "@pinia-plugin-persistedstate/nuxt"
  ],
  colorMode: {
    preference: 'system',
    fallback: 'light',
    hid: 'nuxt-color-mode-script',
    globalName: '__NUXT_COLOR_MODE__',
    componentName: 'ColorScheme',
    classPrefix: '',
    classSuffix: '-mode',
    storage: "cookie",
    storageKey: 'nuxt-color-mode'
  },
  build: {
    transpile: ['@pinia-plugin-persistedstate', /vue-i18n/]
  },
  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "~/assets/scss/base.scss" as *;',
        },
      },
    },
    resolve: {
      alias: {
        'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js'
      }
    },
    plugins: [
      VueI18nVitePlugin({
        include: [
          resolve(dirname(fileURLToPath(import.meta.url)), './locales/*.json')
        ]
      })
    ]
  },
  // Nuxt3运行时全局变量
  runtimeConfig: {
    count :  100,
    public: envData ? envData : {}
  },
  imports: {
    dirs: [
      // 扫描顶级模块
      'composables',
      // ... 或扫描带有特定名称和文件扩展名的一级嵌套模块
      'composables/*/index.{ts,js,mjs,mts}',
      // ... 或扫描给定目录中的所有模块
      'composables/**'
    ]
  },
  devServer: {
    port: 9896,
  },
  nitro: {
    devProxy: {
      "/dev": {
        target: "http://localhost:3000", // 这里是接口地址
        changeOrigin: true,
        prependPath: true,
      },
    },
    // 该配置用于服务端请求转发
    routeRules: {
      '/dev/**': {
        proxy: 'http://localhost:3000/**'
      }
    }
  },
})
